package com.cdx.dxmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdx.dxmall.common.utils.PageUtils;
import com.cdx.dxmall.common.utils.Query;
import com.cdx.dxmall.product.dao.CategoryDao;
import com.cdx.dxmall.product.entity.CategoryEntity;
import com.cdx.dxmall.product.service.CategoryBrandRelationService;
import com.cdx.dxmall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;


    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> entities = baseMapper.selectList(null);

        // 组装父子的树型结构
        List<CategoryEntity> level1Menus = entities.stream().filter((entity) -> {
            return entity.getParentCid() == 0;
        }).map((menu1) -> {
            menu1.setChildren(getChildrenCategory(menu1,entities));
            return menu1;
        }).sorted((m1,m2) -> {
            return (m1.getSort() == null ? 0: m1.getSort()) - (m2.getSort() == null ? 0: m2.getSort()) ;
        }).collect(Collectors.toList());

        return level1Menus;
    }

    @Override
    public void removeMenusByIds(List<Long> ids) {
        //TODO 1.校验菜单是否被引用，如果引用不允许删除
        baseMapper.deleteBatchIds(ids);
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> list = findParentPath(catelogId, paths);
        Collections.reverse(list);
        return list.toArray(new Long[0]);
    }

    @Override
    public void updateDetail(CategoryEntity category) {
        this.updateById(category);
        // 更新关联表的名称
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
    }

    private List<Long> findParentPath(Long catelogId,List<Long> paths){
        paths.add(catelogId);
        CategoryEntity entity = this.getById(catelogId);
        if(entity.getParentCid() != 0) {
            findParentPath(entity.getParentCid(),paths);
        }
        return paths;
    }

    private List<CategoryEntity>getChildrenCategory(CategoryEntity root,List<CategoryEntity> all) {
        List<CategoryEntity> children = all.stream().filter((menu2) -> {
            return menu2.getParentCid().equals(root.getCatId());
        }).map((menu3) -> {
            menu3.setChildren(getChildrenCategory(menu3,all));
            return menu3;
        }).sorted((m1,m2) -> {
            return (m1.getSort() == null ? 0: m1.getSort()) - (m2.getSort() == null ? 0: m2.getSort()) ;
        }).collect(Collectors.toList());
        return children;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

}